package com.example.calendarapp;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * DayFragment class displays the events for a day in a custom listview
 */


public class DayFragment extends Fragment{
	private Calendar c;
	private String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
	private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    private GestureDetector gestureDetector;
    private SimpleOnGestureListener listener;
    private DayFragmentAdapter adapter;
    View.OnTouchListener gestureListener;
    TextView text;
    
    /**
	 * onCreate chains to superclass onCreate
	 * @param Bundle savedInstanceState
	 */
	
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    	    	super.onCreate(savedInstanceState);
    }

    /**
	 * onCreateView sets actionbar navigation to day value, inflates view and sets listview adapter to DayFragmentAdapter object
	 * and sets up swipe detection and onClickMethods for listViewChildren
	 * @param LayoutInflater inflater
	 * @param ViewGroup container
	 * @param Bundle savedInstanceState
	 * 
	 * @return View inflatedView
	 */
	
    
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		ActionBar actionBar = getActivity().getActionBar();
    	actionBar.setSelectedNavigationItem(2);

		c = Calendar.getInstance();
		adapter = new DayFragmentAdapter(getActivity());
		final OtherSwipeDetector sd = new OtherSwipeDetector()
		{
			@Override
			public void left()
			{
				increment();
			}
			
			@Override
			public void right()
			{
				decrement();
			}
		};

		Bundle arguments = this.getArguments();
		if(arguments != null)
		{
			String dateString = arguments.getString("date");
			try
			{
				c.setTime(df.parse(dateString));
				adapter.setDate(dateString);
				adapter.notifyDataSetChanged();
			}catch(Exception ex)
			{
				//lolwut
			}		
		}
		
		 View v = inflater.inflate(R.layout.dayfragment, container, false);

		 
		 
		 ListView lv = (ListView) v.findViewById(R.id.dayList);
		 lv.setAdapter(adapter);
		 lv.setOnItemClickListener(new OnItemClickListener()
		 { @Override
		    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
		    {
			 	if(!sd.swipeDetected())
			 	{
			 		Event e = adapter.getItem(arg2);
			 		if(e != null)
			 		{
			 			FragmentManager fm = getFragmentManager();
						FragmentTransaction ft = fm.beginTransaction();
						EventEditFragment ef = new EventEditFragment();
						Bundle bundle = new Bundle();
						bundle.putInt("id", e.getId());
						bundle.putBoolean("repeated", (e.getRepeatDay() != -1));
						((Fragment)ef).setArguments(bundle);
						ft.replace(R.id.frag, ef);
						ft.addToBackStack(null); 
						ft.commit();
			 		}
			 		else
			 		{
			 			String dateString = df.format(c.getTime());
			 			FragmentManager fm = getFragmentManager();
						FragmentTransaction ft = fm.beginTransaction();
						EventCreateFragment createFrag = new EventCreateFragment();
						Bundle bundle = new Bundle();
						bundle.putString("date", dateString);
						((Fragment)createFrag).setArguments(bundle);
						ft.replace(R.id.frag, createFrag);
						ft.addToBackStack(null);
						ft.commit();
			 		}
			 	}
		    }
		 });
		 lv.setOnTouchListener(sd);
		 text = (TextView) v.findViewById(R.id.dayDateString);
		 text.setText(days[c.get(Calendar.DAY_OF_WEEK)-1] + " " + df.format(c.getTime()));
		 return v;
	}
	
	/**
	 * increments the adapter and gets new data as well as updating the date display
	 */
	
	

    public void increment()
    {
		adapter.incrementDay();
		adapter.notifyDataSetChanged();
		c.add(Calendar.DATE, 1);
		text.setText(days[c.get(Calendar.DAY_OF_WEEK)-1] + " " + df.format(c.getTime()));
    }
    
    /**
	 * decrements the adapter and gets new data as well as updating the date display
	 */
    public void decrement()
    {
		adapter.decrementDay();
		adapter.notifyDataSetChanged();
		c.add(Calendar.DATE, -1);
		text.setText(days[c.get(Calendar.DAY_OF_WEEK)-1] + " " + df.format(c.getTime()));
    }
}


